﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data;
using System.Data.SqlClient;

namespace SQLDAL {
    //该类不存在model类对应，所以不需要partial关键字
    public class BakupAndRestoreDb {
        /// <summary>
        /// 备份数据库
        /// </summary>
        /// <param name="backupPath"></param>
        public void BackDb(string backupPath) {
            if (File.Exists(backupPath)) {
                File.Delete(backupPath);
            }
            string sql = "backup database AddressList to disk = @backuppath";
            SqlParameter[] parameters = {
                new SqlParameter("@backuppath",SqlDbType.VarChar,200)
            };
            parameters[0].Value = backupPath;
            SqlDbHelper.ExecuteNonQuery(sql, CommandType.Text, parameters);
        }
        /// <summary>
        /// 恢复数据库（会提示数据库脱机，恢复不了）
        /// </summary>
        /// <param name="restorePath"></param>
        public void RestoreDb(string restorePath) {
            string sql = "Alter Database AddressList Set Offline with Rollback immediate;use master;restore database AddressList from disk = @restorepath with Replace;Alter Database AddressList Set OnLine With rollback Immediate";
            SqlParameter[] parameters = {
                new SqlParameter("@restorepath",SqlDbType.VarChar,200)
            };
            parameters[0].Value = restorePath;
            SqlDbHelper.ExecuteNonQuery(sql, CommandType.Text, parameters);
        }
    }
}
